package com.sux.demo;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ServerHeartbeatHandler extends ChannelInboundHandlerAdapter {
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.READER_IDLE) { // 读空闲
                System.out.println(simpleDateFormat.format(new Date()) + " 断开与客户端的连接, channel id=" + ctx.channel().id());
                ctx.channel().close();
            } else if (event.state() == IdleState.WRITER_IDLE) { // 写空闲

            } else if (event.state() == IdleState.ALL_IDLE) { // 读写空闲

            }
        }
    }
}
